﻿using CodeReborn.Classes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace CodeReborn
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void readMemory_Click(object sender, EventArgs e)
        {
            XDocument doc = XDocument.Load("MemoryInformation.xml");

            foreach (XElement structureElem in doc.Descendants("Structure"))
            {
                SigScan scanner = new SigScan(
                    Globals.Instance.CurrentProcess,
                    Globals.Instance.ProcessModule.BaseAddress,
                    Globals.Instance.ProcessModule.ModuleMemorySize);

                IntPtr pointer = scanner.FindPattern(
                    (string)structureElem.Attribute("signature"),
                    (int)structureElem.Attribute("offset"));

                foreach (XElement elem in structureElem.Descendants("Property"))
                {
                    List<short> offsets = new List<short>();
                    if ((bool)structureElem.Attribute("pointer"))
                    {
                        offsets.Add(0x0);
                    }
                    byte[] result = new byte[(int)elem.Attribute("size")];
                    offsets.Add(Helpers.HexStringToShort((string)elem.Attribute("offset")));
                    var success = MemoryReader.ReadMemory(
                        Globals.Instance.CurrentProcess,
                        Globals.Instance.ProcessModule,
                        pointer,
                        result,
                        offsets.ToArray<short>()
                        );

                    if (success)
                    {
                        var type = (string)elem.Attribute("type");

                        string value = "N/A";

                        if (type == "string")
                        {
                            value = Encoding.UTF8.GetString(result, 0, result.Length).Trim();
                        }
                        else if (type == "char")
                        {
                            value = BitConverter.ToChar(result, 0).ToString();
                        }
                        else if (type == "short")
                        {
                            value = BitConverter.ToInt16(result, 0).ToString();
                        }
                        else if (type == "int")
                        {
                            value = BitConverter.ToInt32(result, 0).ToString();
                        }
                        else if (type == "float")
                        {
                            value = BitConverter.ToSingle(result, 0).ToString();
                        }

                        dataGridView1.Rows.Add((string)structureElem.Attribute("value") + ":" + (string)elem.Attribute("value"), value);
                    }
                }
            }
        }
    }
}
